package com.ruoyi.system.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.entity.Region;
import com.ruoyi.system.mapper.RegionMappper;
import com.ruoyi.system.service.RegionService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 行政地区业务层实现
 *
 * @author Chopper
 * @since 2020/12/2 11:11
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RegionServiceImpl implements RegionService {

    @Autowired
    private RegionMappper regionMappper;

    @Override
    public List<Region> Regionlist(String id) {
        List<Region> regions = regionMappper.Regionlist(id);
        return regions;
    }
    @Override
    public List<Region> Regionlistzj(String id) {
        List<Region> regions = regionMappper.Regionlistzj(id);
        return regions;
    }


    public Region getId(String id) {
        return regionMappper.getId(id);
    }


    @Override
    @DataScope(deptAlias = "a")
    public List<Region> selectRegionList(Region region) {
        return regionMappper.selectRegionList(region);
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param depts 部门列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<Region> depts) {
        List<Region> deptTrees = buildDeptTree(depts);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }


    /**
     * 构建前端所需要树结构
     *
     * @param regions 部门列表
     * @return 树结构列表
     */
    @Override
    public List<Region> buildDeptTree(List<Region> regions) {
        List<Region> returnList = new ArrayList<Region>();
        List<Long> tempList = new ArrayList<Long>();
        for (Region region : regions) {
            tempList.add(Long.valueOf(region.getDeptId()));
        }
        for (Region dept : regions) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(Long.valueOf(dept.getParentId()))) {
                recursionFn(regions, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty()) {
            returnList = regions;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<Region> list, Region t) {
        // 得到子节点列表
        List<Region> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Region tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Region> list, Region t) {
        return getChildList(list, t).size() > 0;
    }

    /**
     * 得到子节点列表
     */
    private List<Region> getChildList(List<Region> list, Region t) {
        List<Region> tlist = new ArrayList<Region>();
        Iterator<Region> it = list.iterator();
        while (it.hasNext()) {
            Region n = (Region) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().equals(t.getDeptId())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

}
